﻿using System.Collections.Generic;

using Microsoft.WowAddonStudio.Lua.LanguageService.Model;
using Microsoft.WowAddonStudio.Lua.Model;
using Microsoft.WowAddonStudio.Lua.Parser;

namespace Microsoft.WowAddonStudio.Lua.LanguageService.Intellisense
{
    public abstract class QualifiedNameDeclarationProvider : DeclarationProvider
    {
        /// <summary>
        /// Initializes this instance.
        /// </summary>
        protected override void Initialize()
        {
            base.Initialize();
            
            AuthoringSink.EnsureParserSink<QualifiedNameParserSink>();
        }

        protected sealed override IEnumerable<Declaration> GetWords()
         {
            if (Request.HasQualifiedName && Request.QualifiedName.IsQualified)
                return this.GetMemberSelectDeclarations(Request.QualifiedName.Unqualify(true));

            return this.GetDeclarations(Request.QualifiedName);
        }

        protected sealed override IEnumerable<Declaration> GetMembers()
        {
            if (Request.HasQualifiedName)
                return this.GetMemberSelectDeclarations(Request.QualifiedName);

            return base.GetMembers();
        }

        public sealed override IEnumerable<Function> GetMethods()
        {
            if (Request.HasQualifiedName)
                return this.GetMethods(Request.QualifiedName);

            return base.GetMethods();
        }

        /// <summary>
        /// Gets the source of the declaration.
        /// </summary>
        /// <returns>
        /// An instance of the <see cref="DeclarationSource"/> class or null if no declaration source can be provided.
        /// </returns>
        public sealed override DeclarationSource GetDeclarationSource()
        {
            if (Request.HasQualifiedName)
                return GetDeclarationSource(Request.QualifiedName);

            return base.GetDeclarationSource();
        }

        public sealed override DataTip GetDataTip()
        {
            if (Request.HasQualifiedName)
                return GetDataTip(Request.QualifiedName);

            return base.GetDataTip();
        }

        protected virtual IEnumerable<Declaration> GetDeclarations(QualifiedName qualifiedName)
        {
            return new Declaration[0];
        }

        protected virtual IEnumerable<Declaration> GetMemberSelectDeclarations(QualifiedName qualifiedName)
        {
            return new Declaration[0];
        }

        protected virtual IEnumerable<Function> GetMethods(QualifiedName name)
        {
            return new Function[0];
        }

        protected virtual DeclarationSource GetDeclarationSource(QualifiedName qualifiedName)
        {
            return null;
        }

        protected virtual DataTip GetDataTip(QualifiedName qualifiedName)
        {
            return null;
        }
    }
}